﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LibPlateAnalysis;
using System.Reflection;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using HCSAnalyzer.Forms.FormsForGraphsDisplay;
using HCSAnalyzer.Classes.General_Types;

namespace HCSAnalyzer
{

    public partial class FormForOptionsWindow : Form
    {
        public cScreening CurrentScreen;

        public FormForOptionsWindow(cScreening CurrentScreen)
        {
            this.CurrentScreen = CurrentScreen;

            InitializeComponent();
            buttonOk.Focus();
            buttonOk.Select();
            this.comboBoxHierarchicalDistance.SelectedIndex = 0;
            this.comboBoxHierarchicalLinkType.SelectedIndex = 0;
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            this.Visible = false;

            if ((CurrentScreen != null) && (CurrentScreen.ListPlatesActive != null))
            {
                CurrentScreen.GetCurrentDisplayPlate().DisplayDistribution(CurrentScreen.ListDescriptors.CurrentSelectedDescriptorIdx, false);

                CurrentScreen.GlobalInfo.ImageAccessor.NumberOfChannels = (int)this.numericUpDownImageAccessNumberOfChannels.Value;
                CurrentScreen.GlobalInfo.ImageAccessor.InitialPath = this.textBoxImageAccesImagePath.Text;
                if (this.radioButtonImageAccessManual.Checked)
                    CurrentScreen.GlobalInfo.ImageAccessor.ImagingPlatformType = Classes.General_Types.eImagingPlatformType.MANUAL;
                else if (this.radioButtonImageAccessOperetta.Checked)
                    CurrentScreen.GlobalInfo.ImageAccessor.ImagingPlatformType = Classes.General_Types.eImagingPlatformType.OPERETTA;
            }
        }

        private void panel1_DoubleClick(object sender, EventArgs e)
        {
            if (this.colorDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
            else
                panel1.BackColor = this.colorDialog.Color;
        }

        //public override Type BindToType(string assemblyName, string typeName)
        //{
        //    Type typeToDeserialize = null;

        //    String currentAssembly = Assembly.GetExecutingAssembly().FullName;

        //    // In this case we are always using the current assembly
        //    assemblyName = currentAssembly;

        //    // Get the type using the typeName and assemblyName
        //    typeToDeserialize = Type.GetType(String.Format("{0}, {1}", typeName, assemblyName));

        //    return typeToDeserialize;
        //}



        public void Save(string Path)
        {
            Stream stream = null;
            try
            {
                IFormatter formatter = new BinaryFormatter();
                stream = new FileStream(Path, FileMode.Create, FileAccess.Write, FileShare.None);
                formatter.Serialize(stream, this);
            }
            catch
            {
                // do nothing, just ignore any possible errors
            }
            finally
            {
                if (null != stream)
                    stream.Close();
            }
        }


        private void buttonDRCPlateDesign_Click(object sender, EventArgs e)
        {
            if (CurrentScreen == null) return;
            CurrentScreen.GlobalInfo.WindowForDRCDesign.ShowDialog();
        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            if ((CurrentScreen != null) && (CurrentScreen.ListPlatesActive != null))
                CurrentScreen.GetCurrentDisplayPlate().DisplayDistribution(CurrentScreen.ListDescriptors.CurrentSelectedDescriptorIdx, false);
        }

        private void numericUpDownManualMin_ValueChanged(object sender, EventArgs e)
        {
            if (numericUpDownManualMin.Value >= numericUpDownManualMax.Value) numericUpDownManualMin.Value = numericUpDownManualMax.Value;
        }

        private void numericUpDownManualMax_ValueChanged(object sender, EventArgs e)
        {
            if (numericUpDownManualMax.Value <= numericUpDownManualMin.Value) numericUpDownManualMax.Value = numericUpDownManualMin.Value;
        }

        private void radioButtonImageAccessOperetta_CheckedChanged(object sender, EventArgs e)
        {
            groupBoxOperetta.Enabled = true;
            groupBoxManual.Enabled = false;
        }

        private void radioButtonImageAccessManual_CheckedChanged(object sender, EventArgs e)
        {
            groupBoxOperetta.Enabled = false;
            groupBoxManual.Enabled = true;
        }

        private void buttonUpdateImagePath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog OpenFolderDialog = new FolderBrowserDialog();
            OpenFolderDialog.SelectedPath = this.textBoxImageAccesImagePath.Text;

            if (OpenFolderDialog.ShowDialog() != DialogResult.OK) return;
            this.textBoxImageAccesImagePath.Text = OpenFolderDialog.SelectedPath;
        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (CurrentScreen == null) return;
            cGlobalInfoToBeExported GlobalInfoToBeExported = new cGlobalInfoToBeExported(this.CurrentScreen.GlobalInfo);
            GlobalInfoToBeExported.Load("");

            foreach (var item in GlobalInfoToBeExported.DataForcGlobalInfoToBeExported.ListClassNames)
            {
                //this.panelForWellClasses.
            }



        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (CurrentScreen == null) return;
            cGlobalInfoToBeExported GlobalInfoToBeExported = new cGlobalInfoToBeExported(this.CurrentScreen.GlobalInfo);
            GlobalInfoToBeExported.Save("");
        }




    }
}
